$(function () {
	
	$.init();
	var CATEGORY_TOTAL = 6;
	var param = parseUrlSearch();
	var index = !param.category||param.category == 'undefined'?6:param.category;
	$('.tab-link').eq(index*1-1-CATEGORY_TOTAL).addClass('active');
	$('#tab'+index).addClass('active');
	renderProducts(index);
	renderCarCount();

	$(document).on('click', '.j-look', lookHandle);
	// $(document).on('click', '.j-look1', look1Handle);
	$(document).on('click', '.tab-link', tabHandle);
	$(document).on('click', '.filter-more', moreHandle);
	$(document).on('click', '.filter-item', filterHandle);

	function renderProducts (type, country) {
		var map = {
			'1': getProductItem,
			'2': getProductItem,
			'3': getClothHtml,
			'4': getPeakHtml,
			'5': getClothHtml,
			'6': getProductItem
		};
		var _getFn = map[type];
		var html = '';
		fetchProducts(type, country, function (list) {
			html = list.reduce(function (memo, item){
				return memo + (item.status == 2 ? getLowerframe(item) : _getFn(item));
			}, '');
			$('#tab' + type + ' ul').html(html||'暂无商品');
		});
	}

	function fetchProducts (type, country, callback) {
		var param = {
			categoryId: type
		};
		if (country) {
			param.country = country;
		}
		$.ajax({
			url: HOST + '/api/spu/list',
			data: param,
			success: function(res) {
				callback && callback(res.content);
			}
		});
	}

	function getProductItem(data) {
		return '<li class="common_item j-look" data-id="'+data.spuCode+'">\
					<div class="left">\
						<img src="'+data.spuImage+'">\
					</div>\
					<div class="right">\
						<div class="tit">'+data.spuName+'</div>\
						<div><span class="price">'+data.minPrice+'</span>起</div>\
						<div class="info">'+(data.location||data.sexSupport||'')+'</div>\
					</div>\
				</li>';
	}

	function getClothHtml(data) {
		return '<li class="cloth-item j-look" data-id="'+data.spuCode+'">\
					<div class="inner">\
						<div><img src="'+data.spuImage+'"/></div>\
						<div><div class="price">￥<span>'+data.minPrice+'</span>起</div></div>\
						<div class="desc">'+data.spuName+'</div>\
						<div class="saleamount">销量：'+data.saleVolume+'</div>\
						<div class="spec"><span class="button button-light">'+data.sexSupport+'</span>'+data.sizes.reduce(function(memo, item) {
							return memo + '<span class="button button-light">'+item+'</span>'
						}, '')+'</div>\
					</div>'+(data.isHot?'<div class="label"><span>热门</span><div class="left-trian"></div><div class="right-trian"></div></div>':'')+
				'</li>';
	}

	function getPeakHtml(data) {
		return '<li class="cloth-item j-look" data-id="'+data.spuCode+'">\
					<div class="inner">\
						<div><img src="'+data.spuImage+'"/></div>\
						<div class="price">￥<span>'+data.minPrice+'</span></div>\
						<div class="desc">'+data.spuName+'</div>\
						<div class="saleamount">销量：'+data.saleVolume+'</div>\
						<div class="spec">'+data.country.reduce(function(memo, item) {
							return memo + '<span class="button button-light">'+item+'</span>'
						}, '')+'</div>\
					</div>\
				</li>';
	}

	function getLowerframe (data) {
		return '<li class="cloth-item j-look" data-id="'+data.spuCode+'">\
					<div class="inner">\
						<div><img src="'+data.spuImage+'"/></div>\
						<div style="margin-top: 0.4rem;">已下架</div>\
					</div>\
				</li>';
	}

	function lookHandle (e) {
		// var id = $(e.currentTarget).closest('.cloth-item').data('id');
		var id = $(e.currentTarget).closest('.common_item').data('id');
		var category = $('.buttons-tab .active').index() + CATEGORY_TOTAL;
		if (!id) {
			$.alert('没有spu id，请联系管理员！');
			return;
		}
		var url = './product_cloth_info.html?id='+id+'&category='+category;
		window.location = url;
	}

	function look1Handle () {
		$.alert('该商品已下架！');
	}

	function tabHandle (e) {
		renderProducts($(e.currentTarget).index() + 1 + CATEGORY_TOTAL);
	}

	function moreHandle (e) {
		var text = $(e.currentTarget).html();
		$('.filter-list').toggleClass('filter-list-min');
		$(e.currentTarget).html(text == '更多' ? '收起' : '更多');
	}

	function filterHandle (e) {
		var $target = $(e.currentTarget);
		$target.toggleClass('filter-item-full').siblings().removeClass('filter-item-full');
		if ($target.hasClass('filter-item-full')) {
			renderProducts(3, $target.html());
		}else {
			renderProducts(3);
		}
	}

	function fetchCarCount (callback) {
		$.ajax({
			url: HOST + '/api/shopcart/sku/count',
			data: {
				openid: SELFID
			},
			success: function(res) {
				callback && callback(res.content);
			}
		});
	}

	function renderCarCount () {
		fetchCarCount(function (count) {
			$('.car-tip').show().html(count);
		});
	}
})
